package com.winshare.saleAssistant.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.winshare.saleAssistant.domain.dto.req.PageQueryDTO;
import com.winshare.saleAssistant.domain.dto.req.StaffQueryDTO;
import com.winshare.saleAssistant.domain.dto.req.UserQueryDTO;
import com.winshare.saleAssistant.model.SchoolHardware;
import com.winshare.saleAssistant.model.SchoolHardwareMapping;
import com.winshare.saleAssistant.service.ISchoolHardwareMappingService;
import com.winshare.saleAssistant.service.ISchoolHardwareService;
import com.winshare.saleAssistant.service.ISchoolService;
import com.winshare.saleAssistant.util.LogicResponse;
import com.winshare.saleAssistant.util.PageFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 接口获取学校信息
 *
 * @author Donie
 */
@Controller
@RequestMapping("/sys/schoolHardware")
public class SchoolHardwareController {

    @Autowired
    private ISchoolHardwareService schoolHardwareService;
    @Autowired
    private ISchoolService schoolService;
    @Autowired
    private ISchoolHardwareMappingService schoolHardwareMappingService;


    /**
     * 获取列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestBody UserQueryDTO userQueryDTO) {
        return LogicResponse.success(schoolHardwareService.getSchoolByPage(userQueryDTO));
    }

    /**
     * 学校绑定情况
     */
    @RequestMapping(value = "/undistributed")
    @ResponseBody
    public Object undistributed(Integer type, String schoolCode, String schoolId) {
        if (type == null || type == 1) {
            Page<SchoolHardware> page = new PageFactory<SchoolHardware>().defaultPage();
            List<SchoolHardware> schools = schoolHardwareService.getUndistributed(schoolCode, page);
            page.setRecords(schools).getRecords();
            return LogicResponse.success(page);
        } else {
            Page<SchoolHardwareMapping> page = new PageFactory<SchoolHardwareMapping>().defaultPage();
            List<SchoolHardwareMapping> schools = schoolHardwareMappingService.getAllocatedList(schoolId, page);
            page.setRecords(schools).getRecords();
            return LogicResponse.success(page);
        }
    }

    /**
     * 新增
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(SchoolHardware schoolHardware) {
        schoolHardwareService.save(schoolHardware);
        return LogicResponse.success("success");
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer schoolHardwareId) {
        schoolHardwareService.removeById(schoolHardwareId);
        return LogicResponse.success("success");
    }

    /**
     * 修改
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(SchoolHardware schoolHardware) {
        schoolHardwareService.updateById(schoolHardware);
        return LogicResponse.success("success");
    }

    /**
     * 详情
     */
    @RequestMapping(value = "/detail/{schoolHardwareId}")
    @ResponseBody
    public Object detail(@PathVariable("schoolHardwareId") Integer schoolHardwareId) {
        return schoolHardwareService.getById(schoolHardwareId);
    }
}
